package com.ruoyi.collection.process;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.ruoyi.bizsys.domain.CustBankCard;
import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.service.ICustBankCardService;
import com.ruoyi.bizsys.service.ILoanIouService;
import com.ruoyi.collection.domain.ApiParameter;
import com.ruoyi.collection.domain.QueryBankCardReq;
import com.ruoyi.collection.domain.QueryBankCardResp;
import com.ruoyi.collection.process.api.CollectionProcess;
import com.ruoyi.common.constant.UserConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 绑卡列表查询
 * @author Victor
 *
 */
@Slf4j
@Service("QueryBankCard")
public class QueryBankCardPorcessor implements CollectionProcess {
	
    @Autowired
    ILoanIouService loanIouService;
	
	@Autowired
	private ICustBankCardService custBankCardService;
	
	@Override
	public Object process(ApiParameter req) throws Exception {
		
		log.info("绑卡列表查询开始");

		QueryBankCardReq reqBody = JSON.parseObject(req.getBody().toString(), QueryBankCardReq.class);
		QueryBankCardResp respBody = new QueryBankCardResp();
		List<Map<String, Object>> bankCardList = new ArrayList<Map<String, Object>>();
		respBody.setBankCardList(bankCardList);
		
		//获取借据详情
        LoanIou LoanIouQuery = new LoanIou();
        LoanIouQuery.setLoanSerialNo(reqBody.getContrNbr());
        LoanIouQuery.setState(UserConstants.BUSINESS_NORMAL);
        List<LoanIou> loanList = loanIouService.selectLoanIouList(LoanIouQuery);
        if(loanList.size() != 1){
        	log.error("绑卡列表查询时借据数据异常");
        	throw new Exception("借据数据异常");
        }
		
        // 查询银行卡
		CustBankCard custBankCardCondition = new CustBankCard();
		custBankCardCondition.setCustNo(loanList.get(0).getCustNo());
		custBankCardCondition.setState(UserConstants.BUSINESS_NORMAL);
		List<CustBankCard> custBankCards = custBankCardService.selectCustBankCardList(custBankCardCondition);
		
		if(CollectionUtils.isEmpty(custBankCards)){
			return respBody;
		}
		
		for(CustBankCard custBankCard : custBankCards){
			Map<String, Object> bankCard = new HashMap<String, Object>();
			bankCard.put("bankName", custBankCard.getBankName());
			bankCard.put("cardType", custBankCard.getCardType());
			bankCard.put("cardNumber", dstBankCard(custBankCard.getCardNumber()));
			bankCard.put("expireDate", custBankCard.getExpireDate());
			if("Y".equals(custBankCard.getIsDefault())){
				bankCard.put("defaultOrNot", "Yes");
			}else{
				bankCard.put("defaultOrNot", "No");
			}
			
			bankCardList.add(bankCard);
		}
		
		log.info("绑卡列表查询结束");
		
		return respBody;
		
	}

	private Object dstBankCard(String cardNumber) {
		if(StringUtils.isEmpty(cardNumber)){
			return null;
		}
		if(cardNumber.length() <= 6){
			return cardNumber;
		}
		
		StringBuffer sb = new StringBuffer();
		sb.append(cardNumber.substring(0, 6));
		sb.append("**");
		sb.append(cardNumber.substring(6));
		
		return sb.toString();
	}

}
